package com.cloud.wms.ui.inside.replenish;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.widget.TextView;

import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.in.contract.InContract;
import com.cloud.wms.ui.in.entity.TaskDetailEntity;
import com.cloud.wms.ui.in.presnter.InPresenter;
import com.cloud.wms.ui.inside.contract.ReplenishTaskDetailContract;
import com.cloud.wms.ui.inside.entity.ReplenishCommodityEntity;
import com.cloud.wms.ui.inside.entity.ReplenishOrderEntity;
import com.cloud.wms.ui.inside.presenter.ReplenishTaskDetailPresenter;
import com.cloud.wms.ui.main.entity.TaskEntity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @Author:Chen
 * @Date: 2021-01-19 16:45
 * @Description:
 */
public class ReplenishTaskDetailActivity extends BaseBarListActivity<ReplenishCommodityEntity, ReplenishTaskDetailPresenter> implements ReplenishTaskDetailContract.View {

    @BindView(R.id.tv_replenish_code)
    TextView tvReplenishCode;
    @BindView(R.id.tv_replenish_sku)
    TextView tvReplenishSku;
    @BindView(R.id.tv_replenish_amount)
    TextView tvReplenishAmount;
    @BindView(R.id.tv_replenish_create)
    TextView tvReplenishCreate;
    private TaskEntity taskEntity;

    @Override
    protected String barTitle() {
        return "查看详情";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_inside_replenish_task_detail;
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_inside_replenish_task_detail;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        taskEntity = getIntent().getParcelableExtra(IntentKey.OBJECT_KEY);

        mPresenter.loadReplenishOrderDetail(taskEntity.getBillCode());
    }

    @Override
    public void onReload() {
        super.onReload();
        mPresenter.loadReplenishOrderDetail(taskEntity.getBillCode());
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, ReplenishCommodityEntity item) {
        helper.setText(R.id.tv_commodity_name, item.getCommodityName())
                .setText(R.id.tv_pos_code, item.getDestPosCode())
                .setText(R.id.tv_commodity_amount, item.getPlanDownAmount() + "")
                .setText(R.id.tv_commodity_type, item.getStockTypeName().replace("品", ""))
                .setBackgroundResource(R.id.tv_commodity_type, item.getStockType() == 1 ? R.drawable.shape_in_detail_good : R.drawable.shape_in_detail_bad)
                .setTextColorRes(R.id.tv_commodity_name, item.getStockType() == 1 ? R.color.font_green : R.color.font_red);
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onSuccess(Message message) {
        super.onSuccess(message);
        if (message.what == ReplenishTaskDetailPresenter.REPLENISH_DETAIL) {
            ReplenishOrderEntity orderEntity = (ReplenishOrderEntity) message.obj;
            mAdapter.setNewInstance(orderEntity.getCommodityEntities());

            tvReplenishAmount.setText(orderEntity.getNeedAmount());
            tvReplenishCode.setText(orderEntity.getReplenishCode());
            tvReplenishCreate.setText(orderEntity.getCreateDate());
            tvReplenishSku.setText(orderEntity.getNeedSKUAmount());
        }
    }

    public static void start(Context context, TaskEntity entity) {
        Intent intent = new Intent(context, ReplenishTaskDetailActivity.class);
        intent.putExtra(IntentKey.OBJECT_KEY, entity);
        context.startActivity(intent);
    }
}
